package com.kexinde.wjifen.dao;

import java.util.List;

import com.kexinde.wjifen.model.Gift;
import com.kexinde.wjifen.util.JdbcTemplate;
import com.kexinde.wjifen.util.PageUtils;
import com.kexinde.wjifen.vo.PageInfo;

public class GiftDao {

	/**
	 * 查询10个热门微商城礼品兑换,index页显示
	 * 
	 * @return 热门微商城礼品集合，以兑换次数排序
	 * @throws Exception
	 */
	public List<Gift> queryIndexGifts() throws Exception {
		String sql = "select id,name,profile,point,pic,amount,stocks,price,insTime from tb_gift"
				+ " order by amount DESC LIMIT ?,?;";
		Object[] params = { 0, 10 };
		return JdbcTemplate.queryList("com.kexinde.wjifen.model.Gift", sql,
				params, 9);
	}

	/**
	 * 查询list页面中的9个最新商品
	 * @return
	 * 最新商城商品集合，以创建时间排序
	 * @throws Exception
	 */
	public List<Gift> queryNewGifts() throws Exception {
		String sql = "select id,name,profile,point,pic,amount,stocks,price,insTime from tb_gift"
				+ " order by insTime DESC LIMIT ?,?;";
		Object[] params = { 0, 9 };
		return JdbcTemplate.queryList("com.kexinde.wjifen.model.Gift", sql,
				params, 9);
	}
	
	/**
	 * 分页查询热门商城商品
	 * @return
	 * @throws Exception
	 */
	public List<Gift> queryAllHotGifts(PageInfo pageInfo) throws Exception {
		String querySql = "select id,name,profile,point,pic,amount,stocks,price,insTime from tb_gift"
				+ " order by amount DESC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM tb_gift";
		Object [] params1 = {};
		PageUtils pageUtils = new PageUtils(countSql,params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());
		
		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());
		
		Object [] params2 = {pageUtils.getFirst(),pageUtils.getMax()};
		
		return JdbcTemplate.queryList("com.kexinde.wjifen.model.Gift", querySql, params2, 9);
}
	
	/**
	 * 分页查询按积分排序商城商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Gift> queryAllPointGifts(PageInfo pageInfo) throws Exception {
		String querySql = "select id,name,profile,point,pic,amount,stocks,price,insTime from tb_gift"
				+ " order by point ASC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM tb_gift";
		Object[] params1 = {};
		PageUtils pageUtils = new PageUtils(countSql, params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());

		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());

		Object[] params2 = { pageUtils.getFirst(), pageUtils.getMax() };

		return JdbcTemplate.queryList("com.kexinde.wjifen.model.Gift",
				querySql, params2, 9);
	}
	
	
	
	/**
	 * 分页查询按创建时间排序商城商品
	 * 
	 * @return
	 * @throws Exception
	 */
	public List<Gift> queryAllNewGifts(PageInfo pageInfo) throws Exception {
		String querySql = "select id,name,profile,point,pic,amount,stocks,price,insTime from tb_gift"
				+ " order by insTime DESC LIMIT ?,?;";
		String countSql = "SELECT COUNT(*) FROM tb_gift";
		Object[] params1 = {};
		PageUtils pageUtils = new PageUtils(countSql, params1);
		pageUtils.countRange(pageInfo.getCurrentPage(), pageInfo.getLineSize());

		pageInfo.setAmount(pageUtils.getAmount());
		pageInfo.setPageAmount(pageUtils.getPageAmount());

		Object[] params2 = { pageUtils.getFirst(), pageUtils.getMax() };

		return JdbcTemplate.queryList("com.kexinde.wjifen.model.Gift",
				querySql, params2, 9);
	}


	
	/**
	 * 根据giftId查询单个gift对象
	 * @param giftId
	 * @return
	 * @throws Exception
	 */
	public Gift findOneById(Long giftId) throws Exception {
		String sql = "SELECT id,name,profile,point,pic,amount,stocks,price,insTime" +
				" FROM tb_gift WHERE id=?";
		Object[] params = { giftId };
		return JdbcTemplate.query("com.kexinde.wjifen.model.Gift", sql, params, 9);
	}
	
	
	
}
